package game.role;

import game.controller.Entity;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;

import java.util.HashMap;

import static game.basic.GameBase.*;
import static game.basic.GameBase.screenY;
import static game.main.GameApp.player;
import static game.main.GamePanel.ui;

public class NPC_OldMan extends Entity {

    static HashMap<Integer, Image[]> spriteMapNpc = new HashMap<>();
    public static String[] dialogues;//对话数组

    public NPC_OldMan() {
        direction = downDir;
        speed = 2;

        solidAreaX = 10;
        solidAreaY = 20;
        solidAreaWidth = 28;
        solidAreaHeight = 28;
        readSpriteImage("/npc/oldman_%s_%d.png", tileSize, tileSize, spriteMapNpc);
        dialogues = new String[10];
        setDialogue();
    }

    @Override
    public void draw(GraphicsContext gc) {
        if ((worldX) + tileSize > player.worldX - screenX &&
                (worldX) - tileSize < player.worldX + screenX &&
                (worldY) + tileSize > player.worldY - screenY &&
                (worldY) - tileSize < player.worldY + screenY) {
            gc.drawImage(spriteMapNpc.get(direction)[spriteNum],
                    (worldX) - player.worldX + screenX,
                    (worldY) - player.worldY + screenY);
        }
    }

    @Override
    public void update() {
        setAction();
        collisionOn = false;
        cChecker.checkTile(this, currentMap);
        cChecker.checkObject(this, false);
        cChecker.checkEntity(this, npc);
        cChecker.checkEntity(this, monster);
        cChecker.checkPlayer(this);
        super.update();
    }

    public void setDialogue() {
        dialogues[0] = "你好，旅行家。欢迎来到孤树之地。";
        dialogues[1] = "我是孤树老人。我在这里已经很长时间了";
        dialogues[2] = "我从一开始就看到了孤树之地的发展";
        dialogues[3] = "现在，它已经变成了一个充满魔力的世界.现\n在，它已经变成了一个充满魔力的世界";
        dialogues[4] = "你可以在这里找到任何你想要的";
        dialogues[5] = "祝你好运";
    }

    public int dialogueIndex = 0;//对话数组索引

    public void speak() {//说话方法
        if (dialogues[dialogueIndex] == null) {
            dialogueIndex = 0;
        }
        ui.currentDialogue = dialogues[dialogueIndex];
        dialogueIndex++;
        switch (player.direction) {
            case upDir -> direction = downDir;
            case downDir -> direction = upDir;
            case leftDir -> direction = rightDir;
            case rightDir -> direction = leftDir;
        }
    }
}
